package datacrawl.test;



import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	private static C3P0Utils dbcputils = null;
	private ComboPooledDataSource cpds = null;

	private static final Logger LOGGER = Logger.getLogger(C3P0Utils.class);

	private C3P0Utils() {
		if (cpds == null) {
			
			cpds = new ComboPooledDataSource("mysql");
			System.out.println(cpds.getPassword());
			
		}
	}

	public synchronized static C3P0Utils getInstance() {
		if (dbcputils == null)
			dbcputils = new C3P0Utils();
		return dbcputils;
	}

	public Connection getConnection() {
		Connection con = null;
		try {
			con = cpds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}

	//释放链接
	 /**
     * 释放连接
     * @param connection
     */
    public static void releaseConnection(Connection connection){
           try {
                  if(connection != null ) {
                         connection.close();
                  }
           }catch (Exception e) {
                  e.printStackTrace();
           }
    }
	public static void main(String[] args) throws SQLException {
		System.out.println("测试：");
		Connection con = null;
		long begin = System.currentTimeMillis();
		for (int i = 0; i < 100; i++) {
			con = C3P0Utils.getInstance().getConnection();
			LOGGER.info(con);
			con.close();
		}
		long end = System.currentTimeMillis();
		System.out.println(":" + (end - begin) + "ms");
	}
}